package com.csu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.csu.bean.Member;
import com.csu.db.DBUtils;

public class MemberDaoImpl implements MemberDao {

	@Override
	public int loginSelect(int type,String acct, String pwd) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT pwd FROM member WHERE type=? AND acct=?";
			ps = conn.prepareStatement(sql);
		    ps.setInt(1, type);
			ps.setString(2, acct);
			rs = ps.executeQuery();
			if(rs.next() && pwd.equals(rs.getString(1))) {
				return 1; 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return 0;
	}

	@Override
	public int findUser(String acct) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT * FROM member WHERE acct=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, acct);
			rs = ps.executeQuery();
			if(rs.next()) {
				return 1; 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return 0;
	}

	@Override
	public int insertUser(int type, String acct, String pwd) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "INSERT INTO member VALUES(?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, type);
			ps.setString(2, acct);
			ps.setString(3, pwd);
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(null, ps, conn);
		}
		return 0;
	}

	@Override
	public int findPwd(String acct,String pwd) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT pwd FROM member WHERE acct=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, acct);
			rs = ps.executeQuery();
			if(rs.next() && pwd.equals(rs.getString(1))) {
				return 1; 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return 0;
	}

	@Override
	public int updatePwd(String acct, String newPwd) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "UPDATE member SET pwd=? WHERE acct=?";
			ps = conn.prepareStatement(sql);
			ps.setString(2, acct);
			ps.setString(1, newPwd);
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(null, ps, conn);
		}
		return 0;
	}

	@Override
	public int deleteUser(String acct) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "DELETE FROM member WHERE acct=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, acct);
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(null, ps, conn);
		}
		return 0;
	}

	@Override
	public int changeUser(String orgAcct, String newAcct, int newType) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "UPDATE member SET acct=?,type=? WHERE acct=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, newAcct);
			ps.setInt(2, newType);
			ps.setString(3, orgAcct);
			return ps.executeUpdate();
		} catch (SQLException e) {
			//e.printStackTrace();
		}finally{
			DBUtils.close(null, ps, conn);
		}
		return 0;
	}
	
	@Override
	public List<Member> getMemberByPage(int index, int pageSize) {
		List<Member> list = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT * FROM member ORDER BY type LIMIT ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, index);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Member member = new Member();
				member.setType(rs.getInt(1));
				member.setAcct(rs.getString(2));
				list.add(member);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return list;
	}

	@Override
	public int getMemberCount() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT COUNT(*) FROM member";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return 0;
	}

	@Override
	public List<Member> getMemberByPage(String acct, int index, int pageSize) {
		List<Member> list = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT * FROM member WHERE acct=? LIMIT ?,?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, acct);
			ps.setInt(2, index);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Member member = new Member();
				member.setType(rs.getInt(1));
				member.setAcct(rs.getString(2));
				list.add(member);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return list;
	}

	@Override
	public List<Member> getMemberByPage(int type, int index, int pageSize) {
		List<Member> list = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT * FROM member WHERE type=? LIMIT ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, type);
			ps.setInt(2, index);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Member member = new Member();
				member.setType(rs.getInt(1));
				member.setAcct(rs.getString(2));
				list.add(member);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return list;
	}

	@Override
	public int getMemberCount(String acct) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT COUNT(*) FROM member WHERE acct=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, acct);
			rs = ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return 0;
	}

	@Override
	public int getMemberCount(int type) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		conn = DBUtils.getConnection();
		try {
			String sql = "SELECT COUNT(*) FROM member WHERE type=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, type);
			rs = ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(rs, ps, conn);
		}
		return 0;
	}

}
